package com.evaluationsystem.system.serivce.impl;

import cn.hutool.core.date.DateUtil;
import com.evaluationsystem.Utils.ModularNameUtils;
import com.evaluationsystem.Utils.UserContextUtils;
import com.evaluationsystem.system.dao.DictionaryDao;
import com.evaluationsystem.system.dao.PositionAndTeamDao;
import com.evaluationsystem.system.dao.StaffDao;
import com.evaluationsystem.system.entity.Dictionary;
import com.evaluationsystem.system.entity.StaffInfo;
import com.evaluationsystem.system.entity.Tlogs;
import com.evaluationsystem.system.serivce.StaffService;
import com.evaluationsystem.system.serivce.TlogsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StaffServiceImpl implements StaffService {

    @Autowired
    StaffDao dao;

    @Autowired
    DictionaryDao dictionaryDao;

    @Autowired
    UserContextUtils userContextUtils;

    @Autowired
    PositionAndTeamDao positionAndTeamDao;

    @Autowired
    TlogsService logsService;


    @Override
    public Map<String, Object> staffSelectDatas() {
        Map<String,Object> map = new HashMap<>();
        // 职位编号 , 工厂（类型：001）班组编号 是否为评定组（类型：002）
        map.put("positions",positionAndTeamDao.selPositionDelflg());// 职位编号
        map.put("teams",positionAndTeamDao.selTeamDelflg());//班组编号
        map.put("factorys",dictionaryDao.selByType("001"));//工厂（类型：001）
        map.put("appraises",dictionaryDao.selByType("002"));//是否为评定组（类型：002）
        map.put("code",200);
        map.put("msg","ok");
        return map;
    }

    @Override
    public Map<String, Object> staffTableDatas(Map<String, Object> param) {
        /*{page=1, limit=10, vstaffid=, vstaffname=, vpositionid=, vteamid=, vphone=, vappraiseflg=, vfactoryflg=}*/
        Map<String,Object> map = new HashMap<>();
        PageHelper.startPage(Integer.parseInt(param.get("page").toString()), Integer.parseInt(param.get("limit").toString()));

        List<Map<String, Object>> staffInfos =  dao.staffTableDatas(param);

        PageInfo<Map<String, Object>> PageInfos = new PageInfo<>(staffInfos);
        map.put("code","0");//解析接口状态
        map.put("msg","success");//解析提示文本
        map.put("count",PageInfos.getTotal());//解析数据长度
        map.put("data",PageInfos.getList());//解析数据列表
        return map;


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> staffAdd(Map<String, Object> param) {
        Map<String,Object> map = new HashMap<>();
        /*{vstaffid=qwe, vstaffname=qwe, vphone=qwe, vpositionid=ZW0001, vteamid=BZ0001, vfactoryflg=0, vappraiseflg=0}*/
        if (StringUtils.isNotEmpty(dao.selIsNotEmptyStaffid(param.get("vstaffid").toString()))){
            map.put("code",5001);
            map.put("msg","员工编号已存在");
            return map;
        }
        if (StringUtils.isNotEmpty(dao.selIsNotEmptyPhone(param))){
            map.put("code",5003);
            map.put("msg","手机号码已存在");
            return map;
        }
        param.put("vinsuserid",userContextUtils.getUserId());
        param.put("vinsdate", DateUtil.today());
        try {
            int a =dao.insertStaffAdd(param);
            a = logsService.add(new Tlogs()
                    .setVoperationcontent(param.get("vstaffname").toString())/*操作内容*/
                    .setVmodularname(ModularNameUtils.StaffInfoAdd)/*操作模块名称*/
                    .setVpagename(ModularNameUtils.StaffInfo)/*操作页面名称*/
                    .setVoperation(userContextUtils.getUserId())/*操作人*/
                    .setVrecordscode(param.get("vstaffid").toString())
            );
            map.put("code",200);
            map.put("msg","添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            map.put("code",5002);
            map.put("msg","数据访问异常，请联系管理员");
        }
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> staffStartAndForbid(Map<String, Object> param) {
        Map<String,Object> map = new HashMap<>();
        /*{vteamid=BZ0001, vpositionid=ZW0002, vfactoryflg=0, vstaffname=123, vappraiseflg=0, vphone=123,
        vpositionname=22, vforbiddenname=启用, vstaffid=123, vinsdate=2020-04-15, vfactoryflgname=陆友, vinsuserid=gdy,
        vforbidden=1, vteamname=22, vappraiseflgname=否, startAndForbid=staffForbid}*/
        param.put("vupduserid",userContextUtils.getUserId());
        param.put("vupddate",DateUtil.today());
        try {
            int a = dao.upstaffForbiddenByStaffid(param);
            a = logsService.add(new Tlogs()
                    .setVoperationcontent(param.get("startAndForbid").toString())/*操作内容*/
                    .setVmodularname(ModularNameUtils.StaffInfoAdd)/*操作模块名称*/
                    .setVpagename(ModularNameUtils.StaffInfo)/*操作页面名称*/
                    .setVoperation(userContextUtils.getUserId())/*操作人*/
                    .setVrecordscode(param.get("vstaffid").toString())
            );
            map.put("code",200);
            map.put("msg","修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            map.put("code",5001);
            map.put("msg","数据访问异常，请联系管理员");
        }
        return map;
    }

    @Override
    public Map<String, Object> selStaffByStaffid(Map<String, Object> param) {
        Map<String,Object> res = new HashMap<>();
        System.out.println("selStaffByStaffid"+param);
        Map<String,Object> map = dao.selStaffByStaffid(param);
        System.out.println("map"+map);
        res.put("code",200);
        res.put("msg","ok");
        res.put("staff",map);
        return res;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> StaffByStaffid(Map<String, Object> param) {
        Map<String,Object> map = new HashMap<>();
        /*{vstaffid=222, vstaffname=2, vphone=17604530799, vpositionid=ZW0001, vteamid=BZ0010, vfactoryflg=1, vappraiseflg=1}*/
        System.out.println("StaffByStaffid====="+param);

        param.put("key","upata");

        if (StringUtils.isNotEmpty(dao.selIsNotEmptyPhone(param))){
            map.put("code",5003);
            map.put("msg","手机号码已存在");
            return map;
        }
        try {
            param.put("vupduserid",userContextUtils.getUserId());
            param.put("vupddate",DateUtil.today());
            int a = dao.StaffByStaffid(param);
            a = logsService.add(new Tlogs()
                .setVoperationcontent(param.get("vstaffid").toString())/*操作内容*/
                .setVmodularname(ModularNameUtils.StaffInfoAdd)/*操作模块名称*/
                .setVpagename(ModularNameUtils.StaffInfo)/*操作页面名称*/
                .setVoperation(userContextUtils.getUserId())/*操作人*/
                .setVrecordscode(param.get("vstaffid").toString())
            );
            map.put("code",200);
            map.put("msg","修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            map.put("code",5001);
            map.put("msg","数据访问异常，请联系管理员");
        }
        return map;
    }
}











































